/**
 * 
 */
package de.cbf.cam.locations.model.entities;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import de.cbf.cam.locations.model.interfaces.ILocation;

/**
 * @author Stefan Alter
 *
 */
public class ModelLocations {
	
	public static final int PCE_INDEX_rootLocations			= 0;	
	private Collection<ILocation> 	rootLocations 			= new ArrayList<ILocation>();	
	
	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	


	/**
	 * 
	 */
	public ModelLocations() {
	}
	
	public ILocation findLocationFromListByName(String name,Collection<? extends ILocation> locations){
		Iterator<? extends ILocation> iterator = locations.iterator();
		while (iterator.hasNext()) {
			ILocation location = (ILocation) iterator.next();
			if(location.getLocationName().equals(name)){
				return location;
			}
		}
		return null; //not found
	}
	
	public Iterator<? extends ILocation> iteratorRootLocations() {
		return rootLocations.iterator();
	}
	public void setRootLocations(Collection<? extends ILocation> value ) {
		if(this.rootLocations != null && this.rootLocations.equals(value)){ return; }
		Iterator<? extends ILocation> iterator = iteratorRootLocations();
		while (iterator.hasNext()) {
			ILocation oldILocation = (ILocation) iterator.next();
			oldILocation.getPropertyChangeSupport().removePropertyChangeListener(pcaRootLocations);
		}
		this.rootLocations.clear();
		if(rootLocations == null){
			return;
		}
		boolean change = this.rootLocations.addAll(value);		
		if(change){
			iterator = iteratorRootLocations();	
			while (iterator.hasNext()) {
				ILocation newILocation = (ILocation) iterator.next();
				newILocation.getPropertyChangeSupport().addPropertyChangeListener(pcaRootLocations);
			}
			propertyChangeSupport.fireIndexedPropertyChange(
					"rootLocations",PCE_INDEX_rootLocations, null,this.rootLocations);			
		}		
	}
	public boolean addToRootLocations(ILocation newILocation){	
		boolean change = rootLocations.add(newILocation);
		if(change){
			newILocation.getPropertyChangeSupport().addPropertyChangeListener(pcaRootLocations);
			propertyChangeSupport.fireIndexedPropertyChange(
					"rootLocations",PCE_INDEX_rootLocations, null,this.rootLocations);
		}
		return change;		
	}
	public boolean removeFromRootLocations(ILocation oldILocation) {
		boolean change = rootLocations.remove(oldILocation); 
		if(change){
			oldILocation.getPropertyChangeSupport().removePropertyChangeListener(pcaRootLocations);
			propertyChangeSupport.fireIndexedPropertyChange(
					"rootLocations",PCE_INDEX_rootLocations, null,this.rootLocations);
		}
		return change;
	}	
	private PCAdvancementrootLocations pcaRootLocations = new PCAdvancementrootLocations();
	private class PCAdvancementrootLocations implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent event) {
			propertyChangeSupport.fireIndexedPropertyChange(
					event.getPropertyName(),PCE_INDEX_rootLocations,null,rootLocations );
		}		
	}
	
	public boolean removeFromRootLocations(String value){
		//TODO 
		return false;
	}
	
	public boolean addLocationStructureToLocation(ILocation target,ILocation value){
		//TODO 
		return false;
	}

	
	
	/**
	 * @return the propertyChangeSupport
	 */
	public PropertyChangeSupport getPropertyChangeSupport() {
		return propertyChangeSupport;
	}
}
